package seed

import (
	"GraduationProject/public/db"
	"GraduationProject/public/models"
	"GraduationProject/webserver/controllers/util"
	"github.com/gin-gonic/gin"
)

func Add(c *gin.Context) {
	user, err := util.AdminFilter(c)
	if err != nil {
		return
	}

	name := c.PostForm("name")
	url := c.PostForm("url")
	params := c.PostForm("params")
	functionName := c.PostForm("function_name")
	if name == "" || url == "" || functionName == "" {
		util.FailedResponse(c, 10001, "参数不完整")
		return
	}

	if err := db.DB.Create(&models.Seed{
		Loaded:       false,
		UserID:       user.ID,
		Name:         name,
		Url:          url,
		Prams:        params,
		FunctionName: functionName,
	}).Error; err != nil {
		util.FailedResponse(c, 10002, "添加任务失败")
		return
	}

	util.SuccessResponse(c)
}
